package com.ruoyi.system.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.common.SysConstant;
import com.ruoyi.system.domain.GgPzDb;
import com.ruoyi.system.service.IGgPzDbService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import start.spring.basic.log.ServiceLog;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.response.ResponseData;
import start.spring.basic.response.ResponsePage;

/**
 * 
 * @author xuyy
 * @date 2023-08-08
 *
 */
@RestController
@RequestMapping("/dbconfig")
@Api(tags = "数据库配置相关接口")
public class GgPzDbController extends BaseController {
	
    @Autowired
    private IGgPzDbService ggPzDbService;
	
    @ServiceLog(businessType = "查询数据库配置列表", systemType = SysConstant.SYSTEM_TYPE, recordResult = false, userId = "")
    @RequiresPermissions("system:dbconfig:list")
    @PostMapping("/list")
    @ApiOperation(value = "查询数据库配置列表", notes = "分页查询")
    public ResponsePage<GgPzDb> list(PageQuery pageQuery, @RequestBody GgPzDb ggPzDb) {
    	return super.returnResponse(this.ggPzDbService.selectGgPzDbPageList(pageQuery, ggPzDb));
    }
    
    @ServiceLog(businessType = "查询数据库配置详情", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("system:dbconfig:list")
    @PostMapping("/detail")
    @ApiOperation(value = "查询数据库配置详情", notes = "单条记录详情查询")
    public ResponseData<GgPzDb> detail(@RequestBody GgPzDb ggPzDb) {
    	PageResult<GgPzDb> pageResult = this.ggPzDbService.selectGgPzDbPageList(new PageQuery(), ggPzDb);
    	if(pageResult.getRecords()!=null&&!pageResult.getRecords().isEmpty()) {
    		GgPzDb detail = pageResult.getRecords().get(0);
    		return ResponseData.successData(detail);
    	}else {
    		return ResponseData.errorMsg("未查询到详细信息！");
    	}
    }
	
    @ServiceLog(businessType = "修改数据库配置", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("system:dbconfig:edit")
    @PostMapping("/edit")
    @ApiOperation(value = "修改数据库配置", notes = "")
    public ResponseData<Integer> edit(@RequestBody GgPzDb ggPzDb) {
    	return ResponseData.successData(this.ggPzDbService.updateGgPzDb(ggPzDb));
    }
    
    @ServiceLog(businessType = "测试数据库配置", systemType = SysConstant.SYSTEM_TYPE, recordResult = true, userId = "")
    @RequiresPermissions("system:dbconfig:list")
    @PostMapping("/test")
    @ApiOperation(value = "删除redis配置", notes = "")
    public ResponseData<String> remove(@RequestBody List<String> dbIds) {
    	if(dbIds==null||dbIds.size()==0) {
    		return ResponseData.errorMsg("请传入需要测试的数据库唯一标志！");
    	}else {
    		return this.ggPzDbService.testGgPzDbByDbIds(dbIds);
    	}
    }
}
